package com.vogue.supplymall.admin.business.controller;

import com.vogue.supplymall.account.domain.model.AdminUser;
import com.vogue.supplymall.admin.business.common.OpenOrganCheckUtils;
import com.vogue.supplymall.admin.business.domain.biz.OpenOrganBiz;
import com.vogue.supplymall.common.domain.dto.ControllerResultInfo;
import com.vogue.supplymall.common.domain.dto.admin.OrganManagementInfo;
import com.vogue.supplymall.common.domain.dto.admin.OrganSearchInfo;
import com.vogue.supplymall.common.exception.ParamErrorException;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * The type Open organ controller.
 * 开通集团的controller
 *
 * @aouthor: zhangliang
 * @date: Create in 2018-04-27
 */
@Validated
@RestController
public class OpenOrganController extends BaseController {
    /**
     * The constant log.
     */
    public static final Logger LOGGER_OPENORGANCONTROLLER = LoggerFactory.getLogger(OpenOrganController.class);

    @Resource
    private OpenOrganBiz openOrganBiz;

    /**
     * Gets open organ list.
     * 查询未开通的集团列表
     *
     * @param appver          the appver
     * @param organSearchInfo the organ search info
     * @return the open organ list
     */
    @RequestMapping(value = "/is/{appver}/admin/business/notopen/organ/list", method = RequestMethod.POST)
    public ResponseEntity<Object> getNotOpenOrganList(@PathVariable("appver") String appver, @RequestBody OrganSearchInfo organSearchInfo) {
        LOGGER_OPENORGANCONTROLLER.info("get Open Organ List start >>>>>> ");
        // 参数check
        if (null == organSearchInfo.getStart() || null == organSearchInfo.getPages()) {
            throw new ParamErrorException("");
        }

        Map<String, Object> data = openOrganBiz.getOpenOrganList(organSearchInfo);
        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setData(data);
        LOGGER_OPENORGANCONTROLLER.info(" <<<<<< get Open Organ List end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * Gets organ list.
     * 开通集团
     *
     * @param appver         the appver
     * @param managementInfo the management info
     * @return the organ list
     */
    @RequestMapping(value = "/is/{appver}/admin/business/open/organ", method = RequestMethod.POST)
    public ResponseEntity<Object> openOrgan(@PathVariable("appver") String appver, @Validated @RequestBody OrganManagementInfo managementInfo) {
        LOGGER_OPENORGANCONTROLLER.info("Organ List start >>>>>> ");

        AdminUser info = new AdminUser();
        if (SecurityUtils.getSubject().isAuthenticated()) {
            info = (AdminUser) SecurityUtils.getSubject().getPrincipal();
            LOGGER_OPENORGANCONTROLLER.info("获取登信息：" + info.getLoginname());
            if (OpenOrganCheckUtils.apply(managementInfo)) {
                throw new ParamErrorException("");
            }
        } else {
            LOGGER_OPENORGANCONTROLLER.info("获取登信息失败！");
        }

        openOrganBiz.openOrgan(managementInfo, info);
        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setData(null);
        LOGGER_OPENORGANCONTROLLER.info(" <<<<<< Organ List  end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }
}
